package com.elock.service.serviceImpl;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.elock.base.core.Filter;
import com.elock.base.core.Page;
import com.elock.base.core.Pageable;
import com.elock.base.core.service.impl.BaseServiceImpl;
import com.elock.dao.CarDao;
import com.elock.entity.Car;
import com.elock.entity.CarSaleOrg;
import com.elock.entity.Elock;
import com.elock.entity.ElockCar;
import com.elock.entity.SaleOrg;
import com.elock.entity.TerminalCar;
import com.elock.service.CarSaleOrgService;
import com.elock.service.CarService;
import com.elock.service.ElockCarService;
import com.elock.service.ElockService;
import com.elock.service.SaleOrgBaseService;
import com.elock.service.TerminalCarService;

/**
 * 车辆
 * @author 仁德
 */
@Service("carServiceImpl")
public class CarServiceImpl extends BaseServiceImpl<Car> implements CarService {

	@Resource(name = "saleOrgBaseServiceImpl")
	private SaleOrgBaseService saleOrgBaseService;
	@Resource(name = "carSaleOrgServiceImpl")
	private CarSaleOrgService carSaleOrgService;
	@Resource(name = "elockCarServiceImpl")
	private ElockCarService elockCarService;
	@Resource(name = "terminalCarServiceImpl")
	private TerminalCarService terminalCarService;
	@Resource(name = "elockServiceImpl")
	private ElockService elockService;
	@Resource(name = "carDao")
	private CarDao carDao;

	public void saveUpdate(Car car, Long saleOrgId, Long[] elockId, Integer[] berth,
			String[] position) {

		if (car.getId() == null) {
			this.save(car);
		}
		else {
			this.update(car);
		}

		//车辆机构关联
		saveCarSaleOrg(saleOrgId, car);

		//锁车辆关联
		saveElockCar(elockId, berth, position, car);

		//更新终端车辆关系
		updateTerminalCar(car);
	}

	/**
	 * 保存车辆机构关联
	 */
	public void saveCarSaleOrg(Long saleOrgId, Car car) {

		SaleOrg saleOrg = saleOrgBaseService.find(saleOrgId);
		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("car", car));
		List<CarSaleOrg> carSaleOrgs = carSaleOrgService.findList(null, filters, null);
		CarSaleOrg carSaleOrg = null;
		for (CarSaleOrg org : carSaleOrgs) {
			if (saleOrg != null && org.getSaleOrg().equals(saleOrg)) {
				carSaleOrg = org;
			}
			else {
				carSaleOrgService.delete(org);
			}
		}
		if (carSaleOrg == null && saleOrg != null) {
			carSaleOrg = new CarSaleOrg();
			carSaleOrg.setSaleOrg(saleOrg);
			carSaleOrg.setCar(car);
			carSaleOrgService.save(carSaleOrg);
		}
	}

	/**
	 * 保存锁车辆关联
	 */
	public void saveElockCar(Long[] elockId, Integer[] berth, String[] position,
			Car car) {

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("carId", car.getId()));
		List<ElockCar> elockCars = elockCarService.findList(null, filters, null);
		List<Long> oElockIds = new ArrayList<Long>();
		for (ElockCar elockCar : elockCars) {
			oElockIds.add(elockCar.getElockId());
		}

		List<Long> nElockIds = new ArrayList<Long>();
		if (elockId != null && elockId.length > 0) {
			for (Long nElockId : elockId) {
				nElockIds.add(nElockId);
			}
		}

		for (int i = 0; i < nElockIds.size(); i++) {
			Long nElockId = nElockIds.get(i);
			if (!oElockIds.contains(nElockId)) {
				ElockCar elockCar = new ElockCar();
				elockCar.setCarId(car.getId());
				elockCar.setCarNumber(car.getCarNumber());
				elockCar.setElockId(nElockId);
				Elock elock = elockService.find(nElockId);
				elockCar.setElockNum(elock.getElockNum());
				elockCar.setPosition(position[i]);
				elockCar.setBerth(berth[i]);
				elockCarService.save(elockCar);
			}
			else {
				List<Filter> fis = new ArrayList<Filter>();
				fis.add(Filter.eq("elockId", nElockId));
				fis.add(Filter.eq("carId", car.getId()));
				ElockCar elockCar = elockCarService.find(fis);
				if (elockCar != null) {
					elockCar.setCarNumber(car.getCarNumber());
					elockCar.setPosition(position[i]);
					elockCar.setBerth(berth[i]);
					elockCarService.update(elockCar);
				}
			}
		}

		for (ElockCar elockCar : elockCars) {
			if (!nElockIds.contains(elockCar.getElockId())) {
				elockCarService.delete(elockCar);
			}
		}
	}

	/**
	 * 更新终端车辆关联
	 */
	public void updateTerminalCar(Car car) {

		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("carId", car.getId()));
		List<TerminalCar> terminalCars = terminalCarService.findList(null, filters, null);
		for (TerminalCar terminalCar : terminalCars) {
			String carNumber = car.getCarNumber();
			if (!terminalCar.getCarNumber().equals(carNumber)) {
				terminalCar.setCarNumber(carNumber);
				terminalCarService.update(terminalCar);
			}
		}
	}

	public Page<Map<String, Object>> findPage(String carNumber, String carType,
			Long saleOrgId, Pageable pageable) {
		return carDao.findPage(carNumber, carType, saleOrgId, pageable);
	}

	public List<Map<String, Object>> findElockCarByCarId(Long carId) {
		return carDao.findElockCarByCarId(carId);
	}

	public Car findByCarNumber(String carNumber) {
		return carDao.findByCarNumber(carNumber);
	}

	public Page<Map<String, Object>> findElockCar(String carNumber, String carType,
			String elockNum, String elockName, Pageable pageable) {
		return carDao.findElockCar(carNumber, carType, elockNum, elockName, pageable);
	}

	public Page<Map<String, Object>> findCarMonitorPage(String carNumber,
			String elockNumber, Long saleOrgId, String startTime, String endTime,
			Pageable pageable) {
		return carDao.findCarMonitorPage(carNumber,
				elockNumber,
				saleOrgId,
				startTime,
				endTime,
				pageable);
	}

	public Integer countCarMonitor(String carNumber, String elockNumber, Long saleOrgId,
			String startTime, String endTime, Pageable pageable, Integer page,
			Integer size) {
		return carDao.countCarMonitor(carNumber,
				elockNumber,
				saleOrgId,
				startTime,
				endTime,
				pageable,
				page,
				size);
	}

	public List<Map<String, Object>> findCarMonitorList(String carNumber,
			String elockNumber, Long saleOrgId, String startTime, String endTime,
			Pageable pageable, Integer page, Integer size) {
		return carDao.findCarMonitorList(carNumber,
				elockNumber,
				saleOrgId,
				startTime,
				endTime,
				pageable,
				page,
				size);
	}

	public List<Map<String, Object>> findByTerminalId(Long terminalId) {
		return carDao.findByTerminalId(terminalId);
	}
}
